//  Brett Jones
//  PBR Senior Thesis
//  Copyright Brett Jones October 2007

using System;
using System.Collections.Generic;
using System.Text;
using Exocortex.Geometry3D;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace PBR
{
    /// <summary>
    /// Contains all the scene information (lights, paintings etc)
    /// </summary>
    public class Scene: IDisposable
    {
        #region Static Variables

        private const int DEFAULTWIDTH = 640;
        private const int DEFAULTHEIGHT = 480;
        private const float MAXZOOM = 4.0f;
        private const float MINZOOM = 0.25f;

        /// <summary>
        /// After MAXIDLE we do a ComputeVectors
        /// </summary>
        private const int MAXIDLE = 3;

        #endregion
        
        #region Member Variables
        
        // public variables
        public System.Windows.Forms.ToolStripProgressBar ProgressBar;
        public System.Windows.Forms.ToolStripStatusLabel StatusLabel;

        // private variables
        private Size mSize;
        private Light mLight;
        private Painting mPainting;
        private Renderer renderer;

        private string basePath;
        private string projectName;
        private XmlDocument projectSettingsXML;
        private XmlElement projectSettingsXMLRoot;

        /// <summary>
        /// Do we need to do a ComputeVectors?
        /// </summary>
        private bool VectorDirty = false;

        /// <summary>
        /// The number of seconds that we have been idle
        /// </summary>
        private int VectorIdle = 0;

        /// <summary>
        /// The percentage that we are zoomed in on the image
        /// </summary>
        private float zoom = 1.0f;

        /// <summary>
        /// The currently selected segment
        /// </summary>
        public Material SelectedMat;

        /// <summary>
        /// The main form of this program
        /// </summary>
        public MainForm MainForm;

        #endregion

        public Scene(Light light, string basePath, System.Windows.Forms.ToolStripProgressBar progressBar,
            System.Windows.Forms.ToolStripStatusLabel statusLabel, MainForm mainForm)
        {
            this.renderer = new Renderer(this);
            this.mPainting = new Painting(basePath, this);
            this.mLight = light;
            this.mSize = new Size(DEFAULTWIDTH, DEFAULTWIDTH);
            this.basePath = basePath;
            this.ProgressBar = progressBar;
            this.StatusLabel = statusLabel;
            this.MainForm = mainForm;
        }

        /// <summary>
        /// Deconstructor
        /// </summary>
        ~Scene()
        {
            mPainting.Dispose();
        }

        public string ToXml(XmlElement root, XmlDocument doc)
        {
            return "";
        }

        public void ConvertToXml(XmlElement root, XmlDocument doc)
        {
            if(root == null)
            {
                throw new ArgumentNullException("root not initialized");
            }
            if (root.Name != XMLSchema.PBRProjectSettings)
            {
                throw new ArgumentException("root must be" + XMLSchema.PBRProjectSettings);
            }

            XmlElement scene = doc.CreateElement(XMLSchema.Scene);
            root.AppendChild(scene);

            XmlElement size = projectSettingsXML.CreateElement(XMLSchema.Size);
            size.SetAttribute(XMLSchema.Width, mSize.Width.ToString());
            size.SetAttribute(XMLSchema.Height, mSize.Height.ToString());
            scene.AppendChild(size);
        }

        /// <summary>
        /// Creates a new project at the specific project path, this will create and xml
        /// file that stores the relavant project settings, along with a subfolder that
        /// contains all the relavant data for debug purposes.
        /// </summary>
        /// <param name="basePath">The basePath of the folder</param>
        /// <param name="projectName">The project name</param>
        public void NewProject(string basePath, string projectName)
        {
            this.basePath = basePath;
            this.projectName = projectName;

            projectSettingsXML = new XmlDocument();
            //File.WriteAllText(Path.Combine(basePath, projectName), this.ToXml());
        }

        public void Dispose()
        {
            mPainting.Dispose();
        }

        #region Properties

        public Painting Painting
        {
            get
            {
                return mPainting;
            }
            set
            {
                mPainting = value;
            }
        }

        public Light Light
        {
            get
            {
                return mLight;
            }
            set
            {
                mLight = value;
            }

        }

        public Size Size
        {
            get
            {
                return mSize;
            }
            set
            {
                mSize = value;
            }

        }

        public string BasePath
        {
            get
            {
                return basePath;
            }
            set
            {
                basePath = value;
            }
        }

        public Renderer Renderer
        {
            get
            {
                return renderer;
            }
            set
            {
                renderer = value;
            }
        }

        /// <summary>
        /// The percentage that we are zoomed in on the image
        /// Bounded by [MINZOOM, MAXZOOM]
        /// </summary>
        public float Zoom
        {
            get { return zoom; }
            set
            {
                if (value < MINZOOM)
                {
                    zoom = MINZOOM;
                }
                else if (value > MAXZOOM)
                {
                    zoom = MAXZOOM;
                }
                else
                {
                    zoom = value;
                }
            }
        }

        #endregion
}


}
